package ui.control
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import ui.GuiEnum;
	import ui.GuiWnd;

	public class GuiScrollPane extends GuiWnd
	{
		private var objChild: DisplayObject;
		
		private var scrollPaneY:GuiScrollBar;
		private var scrollPaneX:GuiScrollBar;
		
		private var bScale:Boolean;//滚动条是滑块是否缩放
		
		public function GuiScrollPane()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, onEvent);
		}

		override public function init(xml:XML):void
		{
			if(xml.@x)
			{
				this.x = xml.@x;
			}
			
			if(xml.@y)
			{
				this.y = xml.@y;
			}
			
			if(xml.@width)
			{
				this.nWidth = xml.@w;
			}
			
			if(xml.@height)
			{
				this.nHeight = xml.@height;
			}
			
			if(xml.@scale)
			{
				this.isScale = xml.@scale;
			}
		}
		
		override public function addChild(child:DisplayObject):DisplayObject
		{
			if(child is GuiScrollBar)
			{
				addScrollPane(child as GuiScrollBar);
				UpdataBar();
			}
			else if(!objChild)
			{
				objChild = child;
				objChild.x	=0;
				objChild.y	= 0;
				addEvent();
				UpdataBar();
			}
			return super.addChild(child);
		}
		
		private var oldSize:Point = new Point();
		private function onEvent(event:Event):void
		{
			switch(event.type)
			{
				case Event.ADDED_TO_STAGE:
					addEvent();
					break;
				case Event.ENTER_FRAME:
					if(objChild && (objChild.width != oldSize.x || objChild.height != oldSize.y) && (scrollPaneY || scrollPaneX))
					{
						oldSize.x = objChild.width;
						oldSize.y = objChild.height;
						UpdataBar();
					}
					break;
				case Event.REMOVED_FROM_STAGE:
					deleteEvent();
					break;
			}
		}
		
		private function addEvent():void
		{
			if(objChild && !this.hasEventListener(Event.ENTER_FRAME))
			{
				this.addEventListener(Event.ENTER_FRAME, onEvent);
				this.addEventListener(Event.REMOVED_FROM_STAGE, onEvent);
			}
		}	
		
		private function deleteEvent():void
		{
			if(this.hasEventListener(Event.ENTER_FRAME))
			{
				this.removeEventListener(Event.ENTER_FRAME, onEvent);
				this.removeEventListener(Event.REMOVED_FROM_STAGE, onEvent);
			}
		}
		
		public function addScrollPane(bar:GuiScrollBar):void
		{
			if(!scrollPaneY)
			{
				scrollPaneY = bar;
				scrollPaneY.addEventListener(GuiEnum.SCROLLBAR_UPDATA, onUpdataY);
			}
			else if(!scrollPaneX)
			{
				scrollPaneX.rotation = -90;
				scrollPaneX = bar;
				scrollPaneY.addEventListener(GuiEnum.SCROLLBAR_UPDATA, onUpdataX);
			}
		}
		
		protected function onUpdataX(event:Event):void
		{
			updataX();
		}
		
		protected function onUpdataY(event:Event):void
		{
			updataY();
		}
		
		private function updataY():void
		{
			if(!objChild || !scrollPaneY) return;
			
			if(objChild.height <= this.scrollRect.height) return;
			
			var num:Number = scrollPaneY.progressValue;
			
			var value:Number = objChild.height - this.scrollRect.height;
			objChild.y = -(int(num * value));
		}
		
		private function updataX():void
		{
			if(!objChild || !scrollPaneX) return;
			
			if(objChild.width <= this.scrollRect.width) return;
			
			var num:Number = scrollPaneX.progressValue;
			
			var value:Number = objChild.width - this.scrollRect.width;
			objChild.x = -(int(num * value));
		}
		
		/**
		 * 如果子类大小有更新.一定要用这个函数刷新滚动条数据 
		 * 
		 */		
		public function UpdataBar():void
		{
			var bShowBar:Boolean = true;
			if(!objChild) 
			{
				bShowBar = false;
			}

			var father:int 	= 0;
			var child:int 	= 0;
			
			var value:Number;
			var num:Number;
			
			if(scrollPaneY && objChild)
			{
				if(objChild.height > this.scrollRect.height)
				{
					if(isScale)
					{
						father 		= this.scrollRect.height;
						child			=  objChild.height;
						var h:int 	=  int(father*father/child); 
						scrollPaneY.nDragHeight = h / father * scrollPaneY.nHeight ;
					}
					
					num =objChild.height - this.scrollRect.height;
					value = Math.abs(objChild.y) / num;
					scrollPaneY.progressValue = value;
				}
				else
				{
					bShowBar = false;
				}
			}
			
			if(scrollPaneX && objChild)
			{
				if(objChild.height > this.scrollRect.height)
				{
					if(isScale)
					{
						father 		= this.scrollRect.width;
						child			=  objChild.width;
						var w:int 	=  int(father*father/child); 
						scrollPaneX.nDragHeight = w / father * scrollPaneY.nWidth;
					}
					
					num = objChild.width - this.scrollRect.width;
					value = Math.abs(objChild.x) / num;
					scrollPaneX.progressValue = value;
				}
				else
				{
					bShowBar = false;
				}
			}
			
			if(scrollPaneY)
				scrollPaneY.ShowDragButton(bShowBar);

		}

		override public function set nWidth(value:int):void
		{
			super.nWidth = value;
			updataScrollRect();
		}
		
		override public function set nHeight(value:int):void
		{
			super.nHeight = value;
			updataScrollRect();
		}

		private function updataScrollRect():void
		{
			this.scrollRect = new Rectangle(0, 0, nWidth, nHeight);
		}

		public function get isScale():Boolean
		{
			return bScale;
		}

		public function set isScale(value:Boolean):void
		{
			bScale = value;
			UpdataBar();
		}

		
	}
}